﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data.Common;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Text;
using System.Text.RegularExpressions;

namespace CNVP.Framework
{
    public class Paginator
    {
        /// <summary>
        /// 分页超链接字段导航
        /// </summary>
        /// <param name="_PageIndex">当前页</param>
        /// <param name="_PageCount">总页数</param>
        /// <returns>返回一个带超链接翻页地址的html</returns>
        public static string Pagination(int _PageIndex, int _RecordCount, int _PageCount)
        {
            string _url = HttpContext.Current.Request.Url.PathAndQuery;
            _url = Regex.Replace(_url, @"&Page=(.*)|\?Page=(.*)", String.Empty, RegexOptions.IgnoreCase);
            if (Regex.IsMatch(_url, @"\?"))
            {
                _url += @"&";
            }
            else
            {
                _url += @"?";
            }
            int next = 0, pre = 0, startpage = 0, endpage = 0;
            string s_page = "";
            if (_PageIndex < 1) { _PageIndex = 1; }
            if (_PageIndex > _PageCount) { _PageIndex = _PageCount; }
            next = _PageIndex + 1;
            pre = _PageIndex - 1;
            // 中间页起始序号
            startpage = (_PageIndex + 5) > _PageCount ? _PageCount - 9 : _PageIndex - 4;
            if (startpage < 1) { startpage = 1; }
            // 中间页终止序号
            endpage = _PageIndex < 5 ? 10 : _PageIndex + 5;
            if (endpage > _PageCount) { endpage = _PageCount; }
            s_page += "共 " + _RecordCount + " 条记录 页数：" + _PageIndex + "/" + _PageCount + "页 ";
            s_page += _PageIndex > 1 ? "<a href=\"" + _url + "Page=1\">首页</a> <a href=\"" + _url + "Page=" + pre + "\">上一页</a>" : "首页 上一页";
            // 中间页循环显示页码
            for (int i = startpage; i <= endpage; i++)
            {
                s_page += _PageIndex == i ? "<strong>" + i + "</strong>" : "<a class=\"paging_num\" href=\"" + _url + "Page=" + i + "\">" + i + "</a>";
            }
            s_page += _PageIndex != _PageCount ? "<a href=\"" + _url + "Page=" + next + "\">下一页</a> <a href=\"" + _url + "Page=" + _PageCount + "\">末页</a>" : "下一页 末页";
            return s_page;
        }

        /// <summary>
        /// 分页超链接字段导航,适合重写
        /// </summary>
        /// <param name="_PageIndex">当前页</param>
        /// <param name="_PageCount">总页数</param>
        /// <param name="_type">参数</param>
        /// <param name="_typeValue">参数值</param>
        /// <returns>返回一个带超链接翻页地址的html</returns>
        public static string Pagination(int _PageIndex, int _RecordCount, int _PageCount, string _url)
        {
            int next = 0, pre = 0, startpage = 0, endpage = 0;
            string s_page = "";
            if (_PageIndex < 1) { _PageIndex = 1; }
            if (_PageIndex > _PageCount) { _PageIndex = _PageCount; }
            next = _PageIndex + 1;
            pre = _PageIndex - 1;
            // 中间页起始序号
            startpage = (_PageIndex + 5) > _PageCount ? _PageCount - 9 : _PageIndex - 4;
            if (startpage < 1) { startpage = 1; }
            // 中间页终止序号
            endpage = _PageIndex < 5 ? 10 : _PageIndex + 5;
            if (endpage > _PageCount) { endpage = _PageCount; }
            s_page += "共 " + _RecordCount + " 条记录 页数：" + _PageIndex + "/" + _PageCount + "页 ";
            s_page += _PageIndex > 1 ? "<a href=\"" + _url + ".aspx\">首页</a> <a href=\"" + _url + "_" + pre + ".aspx\">上一页</a>" : "首页 上一页";
            // 中间页循环显示页码
            for (int i = startpage; i <= endpage; i++)
            {
                s_page += _PageIndex == i ? "<strong>" + i + "</strong>" : "<a class=\"paging_num\" href=\"" + _url + "_" + i + ".aspx\">" + i + "</a>";
            }
            s_page += _PageIndex != _PageCount ? "<a href=\"" + _url + "_" + next + ".aspx\">下一页</a> <a href=\"" + _url + "_" + _PageCount + ".aspx\">末页</a>" : "下一页 末页";
            return s_page;
        }
    }
}
